$(function () {
    var addSupForm = $("#addSupForm");
    var supCode = $("#supCode");
    var supName = $("#supName");
    var contacts = $("#contacts");
    var supGender = $("#supGender");
    var age = $("#age");
    var address = $("#address");
    var phone = $("#phone");
    var describe = $("#describe");
    var saveSup = $("#saveSup");

    supCode.on({
        focus:function () {
            setStyles(supCode.next(), {'color':'red'}, "(输入供应商编号)", false);
        },
        blur:function () {
            $.ajax({
                type:"get",
                url:getPath() + "/JSP/sup.go",
                data:{method:"supExist", supCode:supCode.val()},
                success:function (data) {
                    console.log(data.supCode);
                    if (data.supCode == "exist") {
                        setStyles(supCode.next(), {"color": 'red'}, getImgCross() + "(该账号已存在)", false);
                    }else{
                        setStyles(supCode.next(), {'color':'#87CEEB'}, getImgTick() + "(该账号可以使用)", true);
                    }
                },
                error:function (data) {
                    setStyles(supCode.next(), {'color':'red'}, getImgCross() + "(请求错误)", false);
                }
            });
        }
    });


    supName.on({
        focus:function () {
            setStyles(supName.next(), {'color':'red'}, "(公司不能为空)", false);
        },
        blur:function () {
            var len = supName.val().length;
            if (len > 0) {
                setStyles(supName.next(), {'color':'#87CEED'}, getImgTick() + "(OK)", true);
            } else{
                setStyles(supName.next(), {'color':'red'}, getImgCross() + "(不能为空)", false);
            }
        }
    });


    contacts.on({
        focus:function () {
            setStyles(contacts.next(), {'color':'red'}, "(姓名不能为空)")
        },
        blur:function () {
            var len = contacts.val().length;
            if (len > 0){
                setStyles(contacts.next(), {'color':'#87CEED'}, getImgTick() + "(OK)", true);
            }else {
                setStyles(contacts.next(), {'color':'red'}, getImgCross() + "(不能为空)", false);
            }
        }
    });

    age.on({
        focus:function () {
            setStyles(age.next(), {'color':'red'}, "(年龄不能为空)")
        },
        blur:function () {
            var dateFormat =/^[0-9]*$/;
            var num = age.val();
            if (dateFormat.test(num)){
                setStyles(age.next(), {'color':'#87CEED'}, getImgTick() + "(OK)", true);
            }else {
                setStyles(age.next(), {'color':'red'}, getImgCross() + "(不能为空)", false);
            }
        }
    });

    address.on({
        focus:function (){
            setStyles(address.next(), {'color':'red'}, "(请输入你的住址)", false);
        },
        blur:function () {
            var val = address.val().length;
            if (val > 0){
                setStyles(address.next(), {'color':'#87CEED'}, getImgTick() + " OK", true);
            }else {
                setStyles(address.next(), {'color':'red'}, getImgCross() + "(不能为空)", false);
            }
        }
    });


    phone.on({
        focus:function () {
            setStyles(phone.next(), {'color':'red'}, "(请输入手机号码)", false);
            console.log("进入输入手机号码");
        },
        blur:function () {
            var phoneFormat = /^1(3[0-9]|4[5,7]|5[0,1,2,3,5,6,7,8,9]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\d{8}$/;
            var p = phone.val();
            if (phoneFormat.test(p)){
                setStyles(phone.next(), {'color':'#87CEED'}, getImgTick() + "(号码符合)", true);
                console.log("号码验证正确");
            }else{
                setStyles(phone.next(), {'color':'red'}, getImgCross() + "(号码不符合,请重新输入)", false);
                console.log("号码验证错误");
            }
        }
    });

    describe.on({
        focus:function (){
            setStyles(describe.next(), {'color':'red'}, "(请输入合作描述)", false);
        },
        blur:function () {
            var val = describe.val().length;
            if (val > 0){
                setStyles(describe.next(), {'color':'#87CEED'}, getImgTick() + " OK", true);
            }else {
                setStyles(describe.next(), {'color':'red'}, getImgCross() + "(不能为空)", false);
            }
        }
    });

    saveSup.on({
        click:function (){
            console.log(supCode.attr("validateStatus"));
            console.log(supName.attr("validateStatus"));
            console.log(contacts.attr("validateStatus"));
            console.log(age.attr("validateStatus"));
            console.log(address.attr("validateStatus"));
            console.log(phone.attr("validateStatus"));
            console.log(describe.attr("validateStatus"));
            if(supCode.attr("validateStatus") != "true"){
                supCode.blur();
            }else if(supName.attr("validateStatus") != "true"){
                supName.blur();
            }else if(contacts.attr("validateStatus") != "true"){
                contacts.blur();
            }else if(age.attr("validateStatus") != "true"){
                age.blur();
            }else if(address.attr("validateStatus") != "true"){
                address.blur();
            }else if(phone.attr("validateStatus") != "true"){
                phone.blur();
            }else if(describe.attr("validateStatus") != "true"){
                describe.blur();
            }else{
                if(confirm("是否确认提交数据")){
                    addSupForm.submit();
                }
            }
        }
    });

})